package com.hyjr.collect.netty.handler;

import com.hyjr.bean.CheckBean;
import com.hyjr.common.utils.LogUtils;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class ReceiveCheckResultServerHandler extends ChannelInboundHandlerAdapter {

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		super.channelActive(ctx);
		LogUtils.debug("采集端，服务端ReceiveCheckResultServerHandler，channelActive！");
	}

	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		super.channelInactive(ctx);
		LogUtils.debug("采集端，服务端ReceiveCheckResultServerHandler，channelInactive！");
		ctx.flush();
		ctx.close();
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		if (msg instanceof CheckBean) {
			CheckBean checkBean = (CheckBean) msg;
			LogUtils.debug("采集端，服务端ReceiveCheckResultServerHandler，channelRead，获取的数据：" + checkBean.toString());
			channelInactive(ctx);
		}
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
		cause.printStackTrace();
		ctx.close();
		LogUtils.debug("采集端，服务端ReceiveCheckResultServerHandler，exceptionCaught", cause);
	}
}
